package jam;

import jam.test.RobotTestBed;
import robocode.control.events.BattleCompletedEvent;
import robocode.control.events.BattleStartedEvent;
import robocode.control.events.TurnEndedEvent;
import robocode.control.snapshot.IRobotSnapshot;
import static org.junit.Assert.assertTrue;

/**
 * A Behavioral test that makes sure RunScared moves in the proximity of each wall on the 
 * battlefield. 
 * 
 * @author John Mack
 * 
 */
public class TestWallProximity extends RobotTestBed {
  boolean topWall = false;
  boolean bottomWall = false;
  boolean rightWall = false;
  boolean leftWall = false;
  int theHeight;
  int theWidth;
  int halfHeight;
  int halfWidth;

  /**
   * Specifies that SittingDuck and RunScared are to be matched up in this test case.
   * 
   * @return The comma-delimited list of robots in this match.
   */
  @Override
  public String getRobotNames() {
    return "sample.Crazy,jam.RunScared";
  }

  /**
   * This test runs for 10 rounds.
   * 
   * @return The number of rounds.
   */
  @Override
  public int getNumRounds() {
    return 10;
  }
  
  /**
   * Sets parameters when battle begins.
   * @param event information for the battle when it begins.
   */
  public void onBattleStarted(BattleStartedEvent event) {
    // Retrieves the battlefield height and width.
    theHeight = event.getBattleRules().getBattlefieldHeight();
    theWidth = event.getBattleRules().getBattlefieldWidth();
    halfHeight = theHeight / 2;
    halfWidth = theWidth / 2;
  }
  
  /**
   *  Specifies where the robot is located at the end of each turn.
   * @param event provides a snapshot of the robots movement at each turn end.
   */
@Override
  public void onTurnEnded(TurnEndedEvent event) {
    IRobotSnapshot robot = event.getTurnSnapshot().getRobots()[1];
    double xCoord = robot.getX();
    double yCoord = robot.getY();

    // If RunScared is in the region of each wall.
    if (yCoord < (theHeight - halfWidth + 50)) {
      topWall = true;
    }
    if (yCoord < halfHeight - 50) {
      bottomWall = true;
    }
    if (xCoord < (theHeight - halfWidth + 100)) {
      rightWall = true;
    }
    if (xCoord < halfWidth - 100) {
      leftWall = true;
    }

  }

  /**
   * Asserts that RunScared moves towards each wall.
   * 
   * @param event specifies details about the completed battle.
   */
  @Override
  public void onBattleCompleted(BattleCompletedEvent event) {
    assertTrue("Robot moved in proximity of Top .", topWall);
    assertTrue("Robot moved in proximity of Bottom Wall.", bottomWall);
    assertTrue("Robot moved in proximity of Right Wall.", rightWall);
    assertTrue("Robot moved in proximity of South Wall.", leftWall);
  }

}
